﻿using LeafCore.Commons.Utils;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LeafCore.Components.RabbitMQ
{
    public static class RabbitMQExtensions
    {
        public static IServiceCollection AddRabbmitMQ(this IServiceCollection services, Action<RabbitMQOptions> options)
        {
            var serviceOptions = new RabbitMQOptions();
            options.Invoke(serviceOptions);

            if (serviceOptions == null || serviceOptions.Host.IsNullOrEmpty() || serviceOptions.Port < 0)
            {
                throw new ArgumentNullException(nameof(options));
            }

            //RabbitMQ服务
            services.AddSingleton(sp => new RabbitMQClient(sp.GetRequiredService<ILogger<RabbitMQClient>>(), serviceOptions));
            services.AddSingleton(sp => new RabbitMQListener(sp.GetRequiredService<ILogger<RabbitMQListener>>(), serviceOptions));

            return services;
        }
    }
}
